package com.caishi.lkx.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.zzw.common.entity.IBSMapper;
import com.caishi.lkx.order.ienum.type.PaymentType;
import com.caishi.lkx.order.mapper.PayImageLogMapper;
import com.caishi.lkx.order.mapper.WeixinPayImageLogMapper;
import com.caishi.lkx.order.mapper.ZfbPayImageLogMapper;
import com.caishi.lkx.order.model.IPayImageLog;
import com.caishi.lkx.order.model.PaymentModel;
import com.caishi.lkx.order.service.IPayImageLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author by keray
 * date:2021/7/15 11:27 上午
 */
@Service
public class PayImageLogServiceImpl implements IPayImageLogService {

    @Resource
    private PayImageLogMapper payImageLogMapper;

    @Resource
    private ZfbPayImageLogMapper zfbPayImageLogMapper;

    @Resource
    private WeixinPayImageLogMapper weixinPayImageLogMapper;


    @Override
    public IBSMapper<? extends IPayImageLog<?>, Long> routerMapper(IPayImageLog.PayImageLog superModel) {
        if (superModel.getLogType() == PaymentType.alipay) {
            return zfbPayImageLogMapper;
        }
        if (superModel.getLogType() == PaymentType.weixin) {
            return weixinPayImageLogMapper;
        }
        return payImageLogMapper;
    }

    @Override
    public IPayImageLog<?> routerEntity(IPayImageLog.PayImageLog superModel) {
        IPayImageLog<?> model = superModel;
        var type = superModel.getLogType();
        if (type == PaymentType.alipay) {
            model = new IPayImageLog.ZFBPayImageLog();
        } else if (type == PaymentType.weixin) {
            model = new IPayImageLog.WeixinPayImageLog();
        } else {
            return model;
        }
        BeanUtil.copyProperties(superModel, model);
        return model;
    }

    @Override
    public IPayImageLog.PayImageLog routerEntityTrans(IPayImageLog<?> entity) {
        var k = new IPayImageLog.PayImageLog();
        BeanUtil.copyProperties(entity, k);
        return k;
    }

    @Override
    public void addLog(PaymentModel paymentModel, Object image) {
        IPayImageLog.PayImageLog payImageLog = new IPayImageLog.PayImageLog();
        payImageLog.setImageData(MapUtil.<String, Object>builder().put("image", image).build());
        payImageLog.setOrderId(paymentModel.getOrderId());
        payImageLog.setCreatedTime(LocalDateTime.now());
        payImageLog.setCreateBy(paymentModel.getPayUserId());
        payImageLog.setLogType(paymentModel.getPaymentType());
        payImageLog.setPayId(paymentModel.getId());
        owner(IPayImageLogService.class).insert(payImageLog);
    }
}
